package sanshui.system.trade.factory.notify;

import freemarker.template.Configuration;
import freemarker.template.Template;
import lombok.extern.slf4j.Slf4j;
import sanshui.system.trade.common.GlobalContain;
import sanshui.system.trade.factory.trade.strategy.StrategyCallType;
import sanshui.system.trade.factory.trade.strategy.StrategyResult;
import sanshui.system.trade.model.NotifyBsModel;

import java.io.File;
import java.io.StringWriter;
import java.util.*;

@Slf4j
public abstract class AbstractTradeSysNotify implements TradeSysNotify{

    @Override
    public String buildContents(List<StrategyResult> strategyResults, GlobalContain.NotifyType notifyType) {
        if (GlobalContain.NotifyType.BS_SINGLE.compareTo(notifyType) == 0){
            // 配置Freemarker
            Configuration configuration = new Configuration(Configuration.VERSION_2_3_31);
            configuration.setClassForTemplateLoading(this.getClass(), "/templates/notify");

            try {
                // 获取模板
                Template template = configuration.getTemplate("notify_bs.ftl");

                // 创建数据模型
                Map<String, Object> dataModel = new HashMap<>();
                List<NotifyBsModel> notifyBsModelList = new ArrayList<>();
                for (StrategyResult strategyResult : strategyResults) {
                    if (Arrays.asList(StrategyCallType.BUY, StrategyCallType.SELL).contains(strategyResult.getStrategyCallType())){
                        NotifyBsModel bsModel = new NotifyBsModel();
                        bsModel.setCode(strategyResult.getCode());
                        bsModel.setPrice(strategyResult.getPrice()/100.0f);
                        bsModel.setStrategyName(strategyResult.getStrategyCode().getMessage());
                        bsModel.setBsTypeName(strategyResult.getStrategyCallType().getDesc());
                        bsModel.setNum(strategyResult.getNum());
                        notifyBsModelList.add(bsModel);
                    }
                }

                if (notifyBsModelList.size() > 0){
                    // 添加一个列表用于循环
                    StringWriter writer = new StringWriter();
                    dataModel.put("dataList", notifyBsModelList);
                    template.process(dataModel, writer);
                    return writer.toString();
                }
                return "";
            }catch (Exception e){
                log.error("模版解析失败, msg:{}", e.getMessage());
            }
        }
        return "";
    }

    public static void main(String[] args) {
        // 配置Freemarker
        Configuration configuration = new Configuration(Configuration.VERSION_2_3_31);
        configuration.setClassForTemplateLoading(AbstractTradeSysNotify.class, "/templates/notify");

        try {
            // 获取模板
            Template template = configuration.getTemplate("notify_bs.ftl");

            // 创建数据模型
            Map<String, Object> dataModel = new HashMap<>();
            List<NotifyBsModel> notifyBsModelList = new ArrayList<>();
            NotifyBsModel bsModel = new NotifyBsModel();
            bsModel.setCode("000001.SZ");
            bsModel.setPrice(9.20f);
            bsModel.setStrategyName("网格交易");
            bsModel.setBsTypeName("买入");
            bsModel.setNum(1000L);
            notifyBsModelList.add(bsModel);

            // 添加一个列表用于循环
            StringWriter writer = new StringWriter();
            dataModel.put("dataList", notifyBsModelList);
            template.process(dataModel, writer);
            System.out.printf(writer.toString());
        } catch (Exception e){

        }
    }
}
